<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* Echarts (https://github.com/apache/echarts)
*********************************************************************-->
<!DOCTYPE html>
<html lang="en-US">

<head>
    <meta charset="UTF-8">
    <title data-i18n='resources.title_chart_iServer'></title>
    <script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
    <script type="text/javascript" include="iclient-leaflet-css,echarts" src="../../dist/leaflet/include-leaflet.js"></script>

    <style>
        .chart-setting {
            position: absolute;
            top: 50px;
            right: 10px;
            width: 450px;
            height: 50px;
            z-index: 800;
            background-color: #fff;
        }

        .chart-type-btn {
            position: absolute;
            top: 8px;
            right: 20px;
            width: 112px;
            height: 36px;
            float: right;
            z-index: 800;
        }

        .chart-setting .input-group {
            left: 16px;
            top: 8px;
            width: 260px;
        }

        .graph {
            margin: 5px;
            width: 26px;
            height: 26px;
            border: none;
            border-radius: 4px;
            background-size: 100%;
            outline: none;
        }

        button {
            float: right;
        }

        #bar {
            background-image: url("../img/bar.png");
        }

        #line {
            background-image: url("../img/ling.png");
        }

        #scatter {
            background-image: url("../img/scatter.png");
        }

        #chart {
            position: absolute;
            top: 100px;
            right: 10px;
            width: 450px;
            height: 350px;
            z-index: 800;
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
    <div class="chart-setting"></div>
    <div id="chart"></div>
    <script>
        init();

        function init() {
            var container = document.getElementsByClassName("chart-setting")[0];
            container.innerHTML = "<div class='input-group'><span class='input-group-addon'>" +
                resources.text_dataset + "</span>" +
                "<select class='form-control' id='rule' name='rule'>" +
                "<option value='https://iserver.supermap.io/iserver/services/data-jingjin/rest/data/datasources/Jingjin/datasets/Landuse_R' selected>Landuse_R@Jingjin</option>" +
                "<option value='https://iserver.supermap.io/iserver/services/data-jingjin/rest/data/datasources/Jingjin/datasets/BaseMap_P'>BaseMap_P@Jingjin</option>" +
                "<option value='https://iserver.supermap.io/iserver/services/map-world/rest/maps/World/layers/Rivers@World@@World'>Rivers@World</option>" +
                "</select></div>" +
                "<div class='chart-type-btn'>" +
                "<button type='button' class='btn btn-default graph' id='scatter' title='" + resources.title_Scatter +
                "'></button>" +
                "<button type='button' class='btn btn-default graph' id='line' title='" + resources.title_GraphLine +
                "'></button>" +
                "<button type='button' class='btn btn-default graph active' id='bar' title='" + resources.title_GraphBar +
                "''></button></div>"
        }

        var host = window.isLocal ? window.server : "https://iserver.supermap.io";
        var map, resultLayer, url = host + "/iserver/services/map-world/rest/maps/World";
        //加载底图
        map = L.map('map', {
            crs: L.CRS.EPSG4326,
            center: [40, 118],
            maxZoom: 18,
            zoom: 6
        });
        new L.supermap.TiledMapLayer(url).addTo(map);
        //图表组件
        var options = {
            type: 'bar',
            datasets: {
                type: 'iServer',
                url: host + "/iserver/services/data-jingjin/rest/data/datasources/Jingjin/datasets/Landuse_R",
                queryInfo: {
                    attributeFilter: "SmID > 0"
                }
            },
            chartOptions: [{
                xAxis: {
                    field: "LANDTYPE",
                    name: "type"
                },
                yAxis: {
                    field: "AREA",
                    name: "Area"
                }
            }]
        }
        var barChart = new L.supermap.Components.Chart("chart", options);
        //加载图表之后，将要素添加到地图上
        barChart.onAdd(addDataToMap);

        function addDataToMap() {
            var features = barChart.getFeatures();
            resultLayer = L.geoJSON(features).addTo(map);
        }

        //为图表类型按钮绑定事件
        bindEvent();

        function bindEvent() {
            $(".graph").on("click", function () {
                $(".graph").removeClass("active");
            });

            $("#bar").on("click", function () {
                $("#bar").addClass("active");
                barChart.changeType('bar');
            });
            $("#line").on("click", function () {
                $("#line").addClass("active");
                barChart.changeType('line');
            });
            $("#scatter").on("click", function () {
                $("#scatter").addClass("active");
                barChart.changeType('scatter');
            });
        }
        //切换数据集
        $("#rule").change(function () {
            var selectedIndex = ($(this).get(0).selectedIndex);
            var url = $(this).val();
            var chartOption, withCredentials, serviceType;
            map.removeLayer(resultLayer);
            if (selectedIndex === 0) {
                chartOption = [{
                    xAxis: {
                        field: "LANDTYPE",
                        name: "type"
                    },
                    yAxis: {
                        field: "AREA",
                        name: "Area"
                    }
                }]
            } else if (selectedIndex === 1) {
                chartOption = [{
                    xAxis: {
                        field: "NAME",
                        name: "NAME"
                    },
                    yAxis: {
                        field: "ADCLASS",
                        name: "ADCLASS"
                    },
                }];
            } else if (selectedIndex === 2) {
                chartOption = [{
                    xAxis: {
                        field: "NAME",
                        name: "name"
                    },
                    yAxis: {
                        field: "KILOMETERS",
                        name: "Kilometers"
                    }
                }];
            } else if (selectedIndex === 3)  {
                chartOption = [{
                    xAxis: {
                        field: "机场",
                        name: "机场"
                    },
                    yAxis: {
                        field: "同比增速%",
                        name: "同比增速%"
                    }
                }];
                serviceType = 'iPortal';
            }
            datasets = {
                type: serviceType, //iServer iPortal 
                url,
                withCredentials, //默认值是false
                queryInfo: {
                    attributeFilter: "SmID > 0"
                }
            }
            barChart.updateData(datasets, chartOption);
        });
    </script>
</body>

</html>